<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1_引出生命周期</title>
</head>
<body>
    <div id="test">

    </div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <script type="text/babel">
        class Life extends React.Component{
            constructor(props){
                super(props)
                console.log('--constructor--')
                this.state = {
                    opacity:1
                }
            }
            componentWillMount(){
                console.log('--componentWillMount--')
            }
            
            death = ()=>{
                console.log('--death--')
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
            testSetState = ()=>{
                console.log('--testSetState--')
                let opacity = this.state.opacity
                opacity -= 0.1
                if(opacity<=0) opacity=1
                this.setState({
                    opacity
                })
            }
            forceTest = ()=>{
                this.forceUpdate()
            }
            shouldComponentUpdate(){
                console.log('--shouldComponentUpdate--')
                return true
            }

            componentWillUpdate(){
                console.log('--componentWillUpdate--')
            }
            render(){
                console.log('--render--');
                return(
                    <div>
                        <h2 style={{opacity:this.state.opacity}}>学习React太有趣了！</h2>
                        <button onClick={this.testSetState}>test setState</button>
                        <button onClick={this.death}>Distroy</button>
                        <button onClick={this.forceTest}>ForceUpdate</button>
                    </div>
                )
            }
            componentDidUpdate(){
                console.log('--componentDidUpdate--')
            }
            componentDidMount(){
                console.log('--componentDidMount--')
                /*
                this.timer = setInterval(()=>{
                    let {opacity} = this.state
                    opacity -= 0.1
                    if(opacity<=0) opacity=1
                    this.setState({
                        opacity:opacity
                    })
                },200)
                */
            }

            componentWillUnmount(){
                console.log('--componentWillUnmount--')
                //clearInterval(this.timer)
            }

        }
        ReactDOM.render(<Life/>,document.getElementById('test'))
    </script>
</body>
</html>